package com.ossjk.oa.accounting.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.accounting.entity.MonthFinance;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.accounting.service.ICwSalaryService;
import com.ossjk.oa.accounting.service.MonthFinanceService;
import com.ossjk.oa.accounting.vo.CwCapitalsVo;
import com.ossjk.oa.accounting.vo.MonthFinanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


@RestController
@RequestMapping("/monthlyreport")
public class MonthlyReportController extends BaseController {
    @Autowired
    private MonthFinanceService monthFinanceService;
    @Autowired
    private ICwCapitalsService iCwCapitalsService;
    @Autowired
    private ICwSalaryService iCwSalaryService;

    //根据部门分类
    @GetMapping("/byDepartment")
    public ResponseBean byDepartment(String mth, HttpServletRequest req) {

        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
//        System.out.println(mth);
        Map map = new HashMap();
        Wrapper wrapper = Condition.create().isWhere(true).like("wagemonth", mth);
        List<MonthFinanceVo> list = monthFinanceService.selectByDepartment(wrapper);
        map.put("navi", monthFinanceService.selectPreOrNextByMth(mth));
        map.put("datas", list);
        return (ResponseBean) responseSuccess(map);
    }

    //根据类型分类
    @GetMapping("/byfenlei")
    public ResponseBean<MonthFinanceVo> byfenlei(String mth) {
        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
//        System.out.println(mth);
        Wrapper wrapper = Condition.create().isWhere(true).like("wagemonth", mth);
        MonthFinanceVo monthFinanceVo = monthFinanceService.selectByfenlei(wrapper);


        return (ResponseBean<MonthFinanceVo>) responseSuccess(monthFinanceVo);
    }

//    //查询每个月的收入与支出
//    @GetMapping("/byshourzhic")
//    public ResponseBean<List<StatisticsVo>> byshourzhic(String mont) {
//        List<StatisticsVo> list = monthFinanceService.selectSrZc();
//        return (ResponseBean<List<StatisticsVo>>) responseSuccess(list);
//
//    }

    @GetMapping("/byGR")
    public ResponseBean byGR(String mth, HttpServletRequest req) {
        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
        Wrapper wrapper = Condition.create().isWhere(true).like("wagemonth", mth);
        List<MonthFinance> list = monthFinanceService.selectGR(wrapper);
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        Map map = new HashMap();
        for (MonthFinance monthFinance : list) {
            list1.add(monthFinance.getGongzi());
            list2.add(monthFinance.getRname());
            map.put("list1", list1);
            map.put("list2", list2);
        }

        return (ResponseBean) responseSuccess(map);
    }

    @GetMapping("/bySRZC")
    public ResponseBean bySRZC() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        System.out.println(year);
        List mth = new ArrayList();
        List shouru = new ArrayList();
        List zhichu = new ArrayList();
        List zhichu1 = new ArrayList();
        Map map = new HashMap();
        List<CwCapitalsVo> list = monthFinanceService.selectSrZc(year);

        for (CwCapitalsVo cwCapitalsVo : list) {
            mth.add(cwCapitalsVo.getMth() + "月");
            shouru.add(cwCapitalsVo.getShouru());
            zhichu.add(cwCapitalsVo.getZhichu());
            zhichu1.add(cwCapitalsVo.getZhichu().multiply(new BigDecimal(-1)));
        }


        map.put("mth", mth);
        map.put("shouru", shouru);
        map.put("zhichu", zhichu);
        map.put("maxMoney", monthFinanceService.selectMaxMoney());
        map.put("zhichu1", zhichu1);
        System.out.println(monthFinanceService.selectMaxMoney());
        return (ResponseBean) responseSuccess(map);
    }


}
